<?php
session_start();
if (empty($_SESSION['user'])){
    die('未登录<a href="login.php">登录</a>');
}else{
    include_once 'mod.php';
    $mod = new mod();
    $mysql = $mod->link();
    $sql = "select * from products";
    $result = $mod->select($mysql,$sql);
    $name = '';
    $cid = '';
    $minprice = '';
    $maxprice = '';
    $status = '';
    $beginDate = '';
    $endDate = '';
    $where = '';
    $flag = ' where ';
    if(!empty($_GET['name'])){
        $name = $_GET['name'];
        $where .= $flag." p.name like '%{$name}%'";
        $flag = ' and ';
    }
    if(!empty($_GET['cid'])){
        $cid = $_GET['cid'];
        $where .= $flag." cid={$cid}";
        $flag = ' and ';
    }
    if(!empty($_GET['minprice'])){
        $minprice =$_GET['minprice'];
        $where .= $flag." price >={$minprice}";
        $flag = ' and ';
    }
    if(!empty( $_GET['maxprice'])){
        $maxprice = $_GET['maxprice'];
        $where .= $flag." price <={$maxprice}";
        $flag = ' and ';
    }
    if(!empty($_GET['status'])){
        $status = $_GET['status'];
        $status_real = 0;
        if($_GET['status'] == 'on'){
            $status_real = 1;
        }
        $where .= $flag." status={$status_real}";
        $flag = ' and ';
    }
    if(!empty($_GET['beginDate'])){
        $beginDate = $_GET['beginDate'];
        $beginDate_stamp = strtotime($beginDate);
        $where .= $flag." create_time>={$beginDate_stamp}";
        $flag = ' and ';
    }
    if(!empty($_GET['endDate'])){
        $endDate = $_GET['endDate'];
        $endDate_stamp = strtotime($endDate)+24*3600-1;
        $where .= $flag." create_time<={$endDate_stamp}";
        $flag = ' and ';
    }

    $sql = 'select p.*,c.`name` c_name from products p inner join category c on c.id=p.cid';
    if(!empty($where)){
        $sql .=$where;
    }

    $result = $mod->select($mysql,$sql);

}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<p><a href="add.php">新增</a></p>
<p>
<form action="" method="get">
    名称：<input type="text" " name="name" value="<?= $name?>">&nbsp;&nbsp;&nbsp;&nbsp;
    分类：
    <select name="cid" >
        <option value="">请选择分类</option>
        <?php foreach ($result as $item){?>
            <option value="<?= $item['id']?>"<?= $cid==$item['id'] ? 'selected'  : '' ?>><?= $item['name']?></option>
        <?}?>

    </select>&nbsp;&nbsp;&nbsp;&nbsp;
    价格：<input type="number" style="width: 100px;" name="minprice" value="<?= $minprice?>">
    --
    <input type="number" style="width: 100px;" name="maxprice" value="<?= $maxprice?>">&nbsp;&nbsp;&nbsp;&nbsp;
    状态：
    <select name="status">
        <option value="">选择状态</option>
        <option value="on"  <?= $status=='on' ? 'selected'  : '' ?>  >上架</option>
        <option value="off"  <?= $status=='off' ? 'selected'  : '' ?>>下架</option>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;
    创建时间：<input type="date" name="beginDate" value="<?= $beginDate?>">
    --
    <input type="date" name="endDate" value="<?= $endDate?>">&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="submit">查询</button>
</form>
</p>
<table border="1" cellspacing="0" cellpadding="10px">
    <tr>
        <th>ID</th>
        <th>产品名称</th>
        <th>分类编号</th>
        <th>价格</th>
        <th>库存</th>
        <th>图片</th>
        <th>状态</th>
        <th>描述</th>
        <th>创建时间</th>
        <th>更新时间</th>
        <th>操作</th>
    </tr>
    <?foreach ($result as $item){?>
        <tr>
            <td><?php echo $item['id']?></td>
            <td><?= $item['name']?></td>
            <td><?= $item['c_name']?></td>
            <td><?= $item['price']?></td>
            <td><?= $item['stock']?></td>
            <td>
                <img height="50px" src="<?= $item['picture']?>">
            </td>
            <td><?= $item['status']==1 ? '上架' : ($item['status']==0 ? '下架' : '异常')   ?></td>
            <td><?= $item['desc']?></td>
            <td><?= date('Y-m-d H:i:s',$item['create_time']) ?></td>
            <td><?= $item['update_time']?></td>
            <td>
                <a href="update.php?id=<?=$item['id']?>">修改</a>
                <a href="delete.php?id=<?=$item['id']?>">删除</a>
            </td>
        </tr>
    <? } ?>
</table>


</body>
</html>

